package cuit.cx.groupcomment.dao;

import java.util.List;
import java.util.Set;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.hibernate.LockMode;
import org.hibernate.Query;
import org.hibernate.criterion.Example;

import cuit.cx.groupcomment.model.Groupon;

/**
 * A data access object (DAO) providing persistence and search support for
 * Groupon entities. Transaction control of the save(), update() and delete()
 * operations can directly support Spring container-managed transactions or they
 * can be augmented to handle user-managed Spring transactions. Each of these
 * methods provides additional information for how to configure it for the
 * desired type of transaction control.
 * 
 * @see cuit.cx.groupcomment.model.Groupon
 * @author MyEclipse Persistence Tools
 */

public class GrouponDAO extends BaseHibernateDAO {
	private static final Log log = LogFactory.getLog(GrouponDAO.class);
	// property constants
	public static final String BUSINISS_ID = "businissId";
	public static final String SHOP_NAME = "shopName";
	public static final String NAME = "name";
	public static final String INTRODUTION = "introdution";
	public static final String PROMPT = "prompt";
	public static final String NOW_PRICE = "nowPrice";
	public static final String OLD_PRICE = "oldPrice";
	public static final String START_TIME = "startTime";
	public static final String END_TIME = "endTime";
	public static final String SHOP_INTRO = "shopIntro";
	public static final String GROUP_TYPE = "groupType";

	public void save(Groupon transientInstance) {
		log.debug("saving Groupon instance");
		try {
			getSession().save(transientInstance);
			log.debug("save successful");
		} catch (RuntimeException re) {
			log.error("save failed", re);
			throw re;
		}
	}

	public void delete(Groupon persistentInstance) {
		log.debug("deleting Groupon instance");
		try {
			getSession().delete(persistentInstance);
			log.debug("delete successful");
		} catch (RuntimeException re) {
			log.error("delete failed", re);
			throw re;
		}
	}

	public Groupon findById(java.lang.Integer id) {
		log.debug("getting Groupon instance with id: " + id);
		try {
			Groupon instance = (Groupon) getSession().get(
					"cuit.cx.groupcomment.model.Groupon", id);
			return instance;
		} catch (RuntimeException re) {
			log.error("get failed", re);
			throw re;
		}
	}

	public List findByExample(Groupon instance) {
		log.debug("finding Groupon instance by example");
		try {
			List results = getSession().createCriteria(
					"cuit.cx.groupcomment.model.Groupon").add(
					Example.create(instance)).list();
			log.debug("find by example successful, result size: "
					+ results.size());
			return results;
		} catch (RuntimeException re) {
			log.error("find by example failed", re);
			throw re;
		}
	}

	public List findByProperty(String propertyName, Object value) {
		log.debug("finding Groupon instance with property: " + propertyName
				+ ", value: " + value);
		try {
			String queryString = "from Groupon as model where model."
					+ propertyName + "= ?";
			Query queryObject = getSession().createQuery(queryString);
			queryObject.setParameter(0, value);
			return queryObject.list();
		} catch (RuntimeException re) {
			log.error("find by property name failed", re);
			throw re;
		}
	}

	public List findByBusinissId(Object businissId) {
		return findByProperty(BUSINISS_ID, businissId);
	}

	public List findByShopName(Object shopName) {
		return findByProperty(SHOP_NAME, shopName);
	}

	public List findByName(Object name) {
		return findByProperty(NAME, name);
	}

	public List findByIntrodution(Object introdution) {
		return findByProperty(INTRODUTION, introdution);
	}

	public List findByPrompt(Object prompt) {
		return findByProperty(PROMPT, prompt);
	}

	public List findByNowPrice(Object nowPrice) {
		return findByProperty(NOW_PRICE, nowPrice);
	}

	public List findByOldPrice(Object oldPrice) {
		return findByProperty(OLD_PRICE, oldPrice);
	}

	public List findByStartTime(Object startTime) {
		return findByProperty(START_TIME, startTime);
	}

	public List findByEndTime(Object endTime) {
		return findByProperty(END_TIME, endTime);
	}

	public List findByShopIntro(Object shopIntro) {
		return findByProperty(SHOP_INTRO, shopIntro);
	}

	public List findByGroupType(Object groupType) {
		return findByProperty(GROUP_TYPE, groupType);
	}

	public List findAll() {
		log.debug("finding all Groupon instances");
		try {
			String queryString = "from Groupon";
			Query queryObject = getSession().createQuery(queryString);
			return queryObject.list();
		} catch (RuntimeException re) {
			log.error("find all failed", re);
			throw re;
		}
	}

	public Groupon merge(Groupon detachedInstance) {
		log.debug("merging Groupon instance");
		try {
			Groupon result = (Groupon) getSession().merge(detachedInstance);
			log.debug("merge successful");
			return result;
		} catch (RuntimeException re) {
			log.error("merge failed", re);
			throw re;
		}
	}

	public void attachDirty(Groupon instance) {
		log.debug("attaching dirty Groupon instance");
		try {
			getSession().saveOrUpdate(instance);
			log.debug("attach successful");
		} catch (RuntimeException re) {
			log.error("attach failed", re);
			throw re;
		}
	}

	public void attachClean(Groupon instance) {
		log.debug("attaching clean Groupon instance");
		try {
			getSession().lock(instance, LockMode.NONE);
			log.debug("attach successful");
		} catch (RuntimeException re) {
			log.error("attach failed", re);
			throw re;
		}
	}
}